/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.components.panels.messageInformationPanel;

import exceptions.NoMessageException;
import processing.logic.Search;
import processing.objects.Message;
import ui.messages.MessageBoxes;

import javax.swing.*;
import java.sql.SQLException;
import java.util.TreeMap;

/**
 * Panel representing Message Information in search Window
 *
 * @author rymek
 *         User: rymek
 *         Date: 18.07.11
 *         Time: 16:18
 */
public class MessageInformationPanel {

    // main panel
    private JPanel messageInfo;

    // objects
    private JComboBox<String> messageList;
    private JTextField msgServiceName;
    private JTextField msgEffDate;
    private JTextField msgTrxType;
    private JTextField msgResponseName;
    private JTextField msgRequestName;
    private JTextField msgChannelName;
    private JTextField msgNodeName;
    private JCheckBox isActive;
    private JCheckBox isCustomPublisher;

    private TreeMap<String, Message> dbMessages;
    private final Search search;

    private Object parentFrame;

    /**
     * Constructor.
     * Also fills message dropdown with current
     * database available messages
     *
     * @param search Search object
     */
    public MessageInformationPanel(Search search) {
        this.search = search;
        this.isActive.setEnabled(false);
        this.isCustomPublisher.setEnabled(false);
        this.fillMessageListDropdown();

        this.messageList.addItemListener(new MessageListItemListener(this));
        this.messageList.getEditor().getEditorComponent().addKeyListener(new MessageListKeyAdapter(this));

    }

    /**
     * Fills message list dropdown with messages
     */
    void fillMessageListDropdown() {

        try {
            this.dbMessages = search.getAllMessages();
            this.messageList.addItem("");
            for (String str : this.dbMessages.keySet()) {
                this.messageList.addItem(str);

            }
        } catch (SQLException e) {
            MessageBoxes.sqlException(e);
        }
    }

    /**
     * Disables fields while Search is running
     */
    public void lockFieldsForSearch() {
        this.messageList.setEditable(false);
        this.messageList.setEnabled(false);
        this.msgChannelName.setEnabled(false);
        this.msgEffDate.setEnabled(false);
        this.msgNodeName.setEnabled(false);
        this.msgRequestName.setEnabled(false);
        this.msgResponseName.setEnabled(false);
        this.msgServiceName.setEnabled(false);
        this.msgTrxType.setEnabled(false);
    }

    /**
     * Enables fields after Search is finished
     */
    public void unlockFields() {
        this.messageList.setEditable(true);
        this.messageList.setEnabled(true);
        this.msgChannelName.setEnabled(true);
        this.msgEffDate.setEnabled(true);
        this.msgNodeName.setEnabled(true);
        this.msgRequestName.setEnabled(true);
        this.msgResponseName.setEnabled(true);
        this.msgServiceName.setEnabled(true);
        this.msgTrxType.setEnabled(true);
    }

    /**
     * Returns Message object selected in the dropdown
     *
     * @return Message object
     * @throws NoMessageException thrown when no message is selected in the message dropdown
     */
    public Message buildMessageInfo() throws NoMessageException {

        if (this.messageSelected()) throw new NoMessageException();

        return this.dbMessages.get(this.messageList.getSelectedItem().toString());
    }

    /**
     * Checks if any message is selected
     *
     * @return true if message is selected
     */
    public boolean messageSelected() {
        return messageList.getSelectedItem().toString().equals("");
    }

    public JComboBox<String> getMessageList() {
        return messageList;
    }

    public JTextField getMsgServiceName() {
        return msgServiceName;
    }

    public JTextField getMsgEffDate() {
        return msgEffDate;
    }

    public JTextField getMsgRequestName() {
        return msgRequestName;
    }

    public JTextField getMsgResponseName() {
        return msgResponseName;
    }

    public TreeMap<String, Message> getDbMessages() {
        return dbMessages;
    }

    public JTextField getMsgTrxType() {
        return msgTrxType;
    }

    public JTextField getMsgChannelName() {
        return msgChannelName;
    }

    public JTextField getMsgNodeName() {
        return msgNodeName;
    }

    public Search getSearch() {
        return search;
    }

    public JPanel getMessageInfo() {
        return messageInfo;
    }

    public JCheckBox getActive() {
        return isActive;
    }

    public JCheckBox getCustomPublisher() {
        return isCustomPublisher;
    }

    public Object getParentFrame() {
        return parentFrame;
    }

    public void setParentFrame(Object parentFrame) {
        this.parentFrame = parentFrame;
    }
}
